package it.bela.pgcdc.service.impl;

import java.util.List;

import it.bela.pgcdc.common.exception.ServiceException;
import it.bela.pgcdc.common.utils.Utilities;
import it.bela.pgcdc.dao.HobbyDao;
import it.bela.pgcdc.domain.Hobby;
import it.bela.pgcdc.service.HobbyService;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
@Service
@Transactional(rollbackFor = ServiceException.class)
public class HobbyServiceImpl implements HobbyService {
	
	private static final Logger log = Logger.getLogger(HobbyServiceImpl.class);
	
	@Autowired(required = true)
	private HobbyDao hobbyDao;
	
	private boolean hasValidKey(Hobby hobby) {
		return (hobby.getId() != 0);
	}

	@Override
	public List<Hobby> findByExample(Hobby hobby) throws ServiceException {
		
		try {
			log.debug("Start list Hobby");
			
			hobby = (Hobby) Utilities.setDefaultParametersSelect(hobby);
			
			List<Hobby> list = hobbyDao.findByExample(hobby);
			
			log.debug("End list Hobby");
			
			return list;

		} catch (Exception e) {
			log.debug("findByExample Hobby failed" + e.getMessage(), e);
			throw new ServiceException(e);
		}
	}

	public List<Hobby> list() throws ServiceException {
		
		try {
			log.debug("Start list");
			
			Hobby hobby = new Hobby();
			
			hobby = (Hobby) Utilities.setDefaultParametersSelect(hobby);

			List<Hobby> list = hobbyDao.findByExample(hobby);
			
			log.debug("End list");
			
			return list;

		} catch (Exception e) {
			log.debug("findByExample failed" + e.getMessage(), e);
			throw new ServiceException(e);
		}
	}

	public boolean delete(Hobby hobby) throws ServiceException {
		log.debug(hobby.getClass().toString() + " - Start delete");
		
		String message = hobby.getClass().toString() + " - delete Hobby failed.";
		try {
			if (!hasValidKey(hobby)) {
				String messageInner = message + " Oggetto senza id.";
				log.debug(messageInner);
				throw new ServiceException(messageInner);
			}
			
			hobby = (Hobby) hobbyDao.findById(hobby.getClass(), hobby.getId());
			hobbyDao.delete(hobby);
			
			log.debug(hobby.getClass().toString() + " - End delete Hobby");
			return true;
		} catch (Exception e) {
			log.debug(message + e.getMessage(), e);
			throw new ServiceException(e);
		}
	}

	public boolean persist(Hobby hobby) throws ServiceException {
		log.debug(hobby.getClass().toString() + " - Start persist Hobby");
		
		String message = hobby.getClass().toString() + " - persist Hobby failed.";
		try {
			
			hobby = (Hobby) Utilities.setDefaultParametersInsert(hobby);
			hobbyDao.persist(hobby);
			
			log.debug(Hobby.class.toString() + " - End persist Hobby");
			return true;
		} catch (Exception e) {
			log.debug(message + e.getMessage(), e);
			throw new ServiceException(e);
		}
	}
	
	public boolean save(Hobby hobby) throws ServiceException {
		log.debug(hobby.getClass().toString() + " - Start save Hobby");
		
		String message = Hobby.class.toString() + " - save Hobby failed.";
		try {
			
			hobby = (Hobby) Utilities.setDefaultParametersInsert(hobby);
			hobbyDao.save(hobby);
			
			log.debug(Hobby.class.toString() + " - End save Hobby");
			return true;
		} catch (Exception e) {
			log.debug(message + e.getMessage(), e);
			throw new ServiceException(e);
		}
	}
	
	public boolean merge(Hobby hobby) throws ServiceException {
		log.debug(Hobby.class.toString() + " - Start merge Hobby");
		
		String message = Hobby.class.toString() + " - merge Hobby failed.";
		try {
			if (!hasValidKey(hobby)) {
				String messageInner = message + " Oggetto senza id.";
				log.debug(messageInner);
				throw new ServiceException(messageInner);
			}
			
			hobby = (Hobby) Utilities.setDefaultParametersUpdate(hobby);
			hobbyDao.merge(hobby);
			
			log.debug(Hobby.class.toString() + " - End merge Hobby");
			return true;
		} catch (Exception e) {
			log.debug(message + e.getMessage(), e);
			throw new ServiceException(e);
		}
	}
	
	public Hobby findById(Class<Hobby> classe, Long id)throws ServiceException {
		log.debug(Hobby.class.toString() + " - Start findById Hobby");
		
		String message = Hobby.class.toString() + " - findById Hobby failed.";
		try {
			
			Hobby hobby = (Hobby) hobbyDao.findById(classe, id);
			
			log.debug(Hobby.class.toString() + " - End findById Hobby");
			return hobby;
		} catch (Exception e) {
			log.debug(message + e.getMessage(), e);
			throw new ServiceException(e);
		}
	}
}
